package com.stan.script.handle.win;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

/**
 * @author Junqiang Zhu
 * @since 2024-05-09 11:22
 */
public class Test {

    private static String getFoldPath(String name) {
        return "E:\\file\\company\\project\\23干预\\血管动画613\\血管动画\\血脂\\" + name;
    }

    private static String getToPath(String name) {
        return "E:\\file\\company\\project\\23干预\\血管动画613-gif\\血脂\\" + name;
    }

    public static void main(String[] args) {
        List<String> list = Arrays.asList("天累积", "周累积", "季累积", "年累积");
        list.forEach(name -> {
            String fromPath = getFoldPath(name);
            String toPath = getToPath(name);
            handleResourceFile(fromPath, toPath);
        });
    }

    private static void handleResourceFile(String folderPath, String targetPath) {
        try {
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Resource[] resources = resolver.getResources("file:" + folderPath + "/*");

            for (Resource resource : resources) {
                if (!StringUtils.hasText(resource.getFilename())) {
                    continue;
                }
                String[] split = resource.getFilename().split("\\.");
                change2(split[0], folderPath, targetPath);
            }
        } catch (IOException e) {
            System.out.println("错误");
        }
    }

    public static void change2(String fileName, String sourcePath, String targetPath) {
        Thread thread = new Thread(() -> {
            System.out.println("处理" + fileName);
            try {

//                String command = "ffmpeg -y -i E:\\file\\company\\project\\23干预\\动画改\\" + fileName + ".mkv E:\\file\\company\\project\\23干预\\gif\\" + fileName + ".gif";
                String command = "ffmpeg -y -i " + sourcePath + "\\" + fileName + ".mkv " + targetPath + "\\" + fileName + ".gif";
                Process process = Runtime.getRuntime().exec(command);

                // 启动新线程处理进程输出流
                Thread outputThread = new Thread(() -> {
                    try {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                        String line;
                        while ((line = reader.readLine()) != null) {
                            // 打印输出日志
                            System.out.println(line);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
                outputThread.start();

                process.waitFor(); // 等待进程执行完毕
            } catch (Exception e) {
                System.out.println("发生错误");
                e.printStackTrace();
            }
        });
        thread.start();
    }
}
